package com.jiaanpei.frameproject.modle.appupdate;

import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.NotificationCompat;

import com.jiaanpei.frameproject.R;
import com.jiaanpei.frameproject.app.AppApplication;
import com.jiaanpei.frameproject.app.Constants;
import com.jiaanpei.frameproject.base.BaseActivity;
import com.jiaanpei.frameproject.manage.ActivityManager;
import com.jiaanpei.frameproject.modle.bean.appupdate.AppUpdateBean;
import com.jiaanpei.frameproject.utils.Utils;
import com.jiaanpei.frameproject.widget.MyAPPUpdateDialog;

import java.io.File;
import java.util.Timer;
import java.util.TimerTask;

public class AppUpdateManager {

    protected NotificationCompat.Builder nbuilder;

    protected Timer timer;
    protected TimerTask task;
    protected int progress,max;
    protected NotificationManager nm ;
    protected DownloadInfo downloadInfoHome;
    BaseActivity ctx;
    MyAPPUpdateDialog updateDialog;
    private static volatile AppUpdateManager singleton;

    private AppUpdateManager() {}

    public static AppUpdateManager getInstance() {
        if (singleton == null) {
            synchronized (AppUpdateManager.class) {
                if (singleton == null) {
                    singleton = new AppUpdateManager();
                }
            }
        }
        return singleton;
    }

    protected void initNotification(BaseActivity ctx) {
        nm = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
        nbuilder = new NotificationCompat.Builder(AppApplication.instance);
        nbuilder.setContentTitle("正在更新...") //设置通知标题
                .setSmallIcon(R.mipmap.ic_launcher)
//                .setLargeIcon(BitmapFactory.decodeResource(AppApplication.instance.getResources(), R.mipmap.icon_main)) //设置通知的大图标
                .setDefaults(Notification.DEFAULT_LIGHTS) //设置通知的提醒方式： 呼吸灯
                .setPriority(NotificationCompat.PRIORITY_MAX) //设置通知的优先级：最大
                .setAutoCancel(true)//设置通知被点击一次是否自动取消
                .setContentText("下载进度:" + "0%")
                .setProgress(100, 0, false)
        ;
        nm.notify(ctx.NM, nbuilder.build());
    }



    public  void downMyApp(BaseActivity ctx,String url,MyAPPUpdateDialog updateDialog) {
        this.ctx=ctx;
        this.updateDialog=updateDialog;
        initNotification(ctx);

        timer = new Timer();

        DownloadManager.getInstance(url).download(url, new DownLoadObserver() {//"http://210.82.76.66:8081/IXC899052538373595c2cbfeb9ca8d70327/cf86939a6e29c25da7f6892bf3bcfefc.apk?attname=app-JIA_AN_PEI_TEST-release.apk&sign=22d84ac1a438ac92650acf0268c9998d&t=5c62394e"
            @Override
            public void onNext(DownloadInfo value) {
                super.onNext(value);
                progress = (int) value.getProgress();
                max = (int) value.getTotal();
                if (task != null) {
                    task.cancel();  //将原任务从队列中移除
                }
                task = new TimerTask() {
                    @Override
                    public void run() {
                        downLoadApphandler.sendEmptyMessage(0x111);

                    }
                };
                if(progress < max){
                    timer.schedule(task, 0, 100);
                }



            }

            @Override
            public void onComplete() {
                if (downloadInfo != null&&updateDialog!=null) {
                    downloadInfoHome=downloadInfo;
                    updateDialog.pb.setMax(max);
                    updateDialog.pb.setProgress(max);
                    task.cancel();
                    timer.cancel();
                    nbuilder.setContentTitle("下载完成")
//                            .setContentText("点击安装")
                            .setProgress(100, (int) 100, false)
                            .setContentText("下载进度:100%")
                            .setAutoCancel(true);//设置通知被点击一次是否自动取消
                    nm.notify(ctx.NM, nbuilder.build());
                    Utils.collapseStatusBar(ctx);
                    File file=new File(Constants.BASEPATH, downloadInfo.getFileName());
                    Utils.installApk(file,ctx,nm);
                    updateDialog.dismiss();
                }
            }
        });
    }


    Handler downLoadApphandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(updateDialog!=null){
                updateDialog.pb.setMax(max);
                updateDialog.pb.setProgress(progress);
                nbuilder.setProgress(100, (int) (progress * 100 / max), false);
                nbuilder.setContentText("下载进度:" + (int) (progress * 100/ max ) + "%");
                nm.notify(ctx.NM, nbuilder.build());
            }
        }
    };


}
